package register

import (
	"bytes"
	"encoding/json"
	"ez/config/code"
	"fmt"
	"gitee.com/dreamwood/ez-go/ez"
	"io"
	"net/http"
	"os"
)

func init() {
	//
	//task.NewTask("ez-heart-beat", func() {
	//	HeartBeatNsq()
	//}).SetEvery(5)
	//
	//ez.Subscribe(ez.EventAfterServerRun, func(v interface{}, ctx context.Context) {
	//	RegService()
	//})
}

type ServiceConfig struct {
	App struct {
		Id   string `yaml:"id"`
		Name string `yaml:"name"`
	} `yaml:"app"`
	Dependence  map[string]string `yaml:"dependence"`
	LoadBalance struct {
		Name   string `yaml:"name"`
		Weight int    `yaml:"weight"`
	} `yaml:"load-balance"`
	Register struct {
		Host             string `yaml:"host"`
		Auth             string `yaml:"auth"`
		HearBeatInterval int    `yaml:"hear-beat-interval"`
	} `yaml:"register"`
	Routes []struct {
		From string `yaml:"from"`
		To   string `yaml:"to"`
		Sort int    `yaml:"sort"`
		Port string `yaml:"port"`
	} `yaml:"routes"`
	//以下非配置項
	RunPath string
	Host    string
	Port    string
	Pid     int
}

func RegService() {
	heartBeatUrl := fmt.Sprintf("http://127.0.0.1:4721/_reg_service")

	conf := new(ServiceConfig)
	conf.LoadBalance.Name = "main"
	conf.LoadBalance.Weight = 120
	conf.App.Id = ez.ConfigService.AppId
	conf.App.Name = ez.ConfigService.AppName
	routes := make([]struct {
		From string `yaml:"from"`
		To   string `yaml:"to"`
		Sort int    `yaml:"sort"`
		Port string `yaml:"port"`
	}, 0)
	routes = append(routes, struct {
		From string `yaml:"from"`
		To   string `yaml:"to"`
		Sort int    `yaml:"sort"`
		Port string `yaml:"port"`
	}{
		From: "^/erp:any(.+)",
		To:   "/erp$any",
		Sort: 100,
		Port: "555",
	})
	conf.Routes = routes
	conf.RunPath = os.Args[0]
	conf.Host = ez.ConfigServer.ServerHost
	conf.Port = fmt.Sprintf("%d", ez.ConfigServer.ServerPort)
	conf.Pid = os.Getpid()

	data, e := json.Marshal(conf)
	if ez.Try(e) {
		ez.Get(e)
		return
	}
	bf := bytes.NewBuffer(data)

	resp, e := http.Post(heartBeatUrl, "application/json", bf)
	if ez.Try(e) {
		ez.Get(e)
		return
	}
	body, e := io.ReadAll(resp.Body)
	if ez.Try(e) {
		ez.Get(e)
		return
	}
	message := new(ez.JsonOut)
	e = json.Unmarshal(body, message)
	if ez.Try(e) {
		return
	}
	if message.Code != code.Success {
		ez.LogToConsole(message.Message)
	} else {
		ez.LogToConsole(message.Message)
	}
}

func HeartBeatNsq() {
	conf := new(ServiceConfig)
	conf.LoadBalance.Name = "main"
	conf.LoadBalance.Weight = 120
	conf.App.Id = ez.ConfigService.AppId
	conf.App.Name = ez.ConfigService.AppName
	conf.RunPath = os.Args[0]
	conf.Host = ez.ConfigServer.ServerHost
	conf.Port = fmt.Sprintf("%d", ez.ConfigServer.ServerPort)
	conf.Pid = os.Getpid()
	routes := make([]struct {
		From string `yaml:"from"`
		To   string `yaml:"to"`
		Sort int    `yaml:"sort"`
		Port string `yaml:"port"`
	}, 0)
	routes = append(routes, struct {
		From string `yaml:"from"`
		To   string `yaml:"to"`
		Sort int    `yaml:"sort"`
		Port string `yaml:"port"`
	}{
		From: "^/erp:any(.+)",
		To:   "/erp$any",
		Sort: 100,
		Port: "555",
	})
	conf.Routes = routes
	data, e := json.Marshal(conf)
	if ez.Try(e) {
		ez.Debug(e)
		return
	}
	//bf := bytes.NewBuffer(data)
	e = ez.NsqProducer.Publish("_heart_beat", data)
	if ez.Try(e) {
		ez.Debug(e)
		return
	}
}
